#include "graphicsrectitem.h"
#include <QGraphicsSceneMouseEvent>
#include <QLinearGradient>
#include <QPainter>
#include <QStyleOptionGraphicsItem>

GraphicsRectItem::GraphicsRectItem() :
    _rotation(0)
{
    setFlag(ItemIsMovable);
}

void GraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    _startPos = mapToScene(event->pos());
    QGraphicsItem::mousePressEvent(event);
}

void GraphicsRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF pos = mapToScene(event->pos());
    pos.y() > _startPos.y() ? ++_rotation : --_rotation;
    setRotation(_rotation);
    _startPos = pos;
}

void GraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
    QLinearGradient gradient(0, 0, 15, 15);
    gradient.setColorAt(0, Qt::white);
    gradient.setColorAt(1, Qt::gray);

    QBrush brush(gradient);
    QTransform t;
    brush.setTransform(t.rotate(-rotation()));

    painter->setBrush(brush);
    painter->drawRect(-40, -30, 80, 60);
}
